// DOM 类名操作简单封装
function addClass(e, classname) {
	if (!containsClass(e, classname)) {
		e.classList.add(classname);
	}
}
function removeClass(e, classname) {
	if (containsClass(e, classname)) {
		e.classList.remove(classname);
	}
}
function containsClass(e, classname) {
	return e.classList.contains(classname)
}
// 一些数学函数的封装
function getRandomRange(x, y, num) {
	if (num > 1) {
		let res = [];
		for (let i = x; i <= y; i++) {
			res.push(i);
		}
		let final = [];
		for (let j = 0; j < num; j++) {
			final.push(res.splice(getRandomRange(0, res.length - 1), 1))
		}
		return final;
	} else {
		return Number(Math.round(Math.random() * (y - x)) + x);
	}
}
const anim = (t, m, h) => {
	let _tar = ".battleScreen";
	switch (t) {
		case 'm':
			_tar = " .battleMonster"
			break;
		case 'h':
			_tar += " .battleHero"
			break;
		default:
			_tar += ` .battleHeroPet[id="${t}"]`
			break;
	}
	_tar = document.querySelector(_tar);
	if (!_tar) {
		console.log("动画错误", t, m, h);
		return;
	}
	const _class = ["animate__animated", '', "animate__fast"];
	if (m == 'dead') {
		_tar.innerText = h ? (-h + "阵亡") : "阵亡";
		_class[1] = "animate__fadeOutDown";
		_tar.classList.add(..._class);
		const deadCallback = () => {
			_tar.innerText = '';
			_tar.classList.remove(..._class);
			_tar.style.display = "none";
			_tar.removeEventListener('animationend', deadCallback)
		};
		_tar.addEventListener('animationend', deadCallback)
	} else {
		switch (m) {
			case 'attack':
				if (t == 'm') {
					_class[1] = "animate__slideInRight"
				} else {
					_class[1] = "animate__slideInLeft"
				}
				_tar.innerText = '发起攻击'
				break;
			case 'shake':
				_class[1] = "animate__shakeX"
				// 飘血
				_tar.innerText = -h;
				break;
			case 'crit':
				_class[1] = "animate__wobble";
				_tar.innerText = '暴击'
				break;
			case 'away':
				if (t == 'm') {
					_class[1] = "animate__slideOutRight"
				} else {
					_class[1] = "animate__slideOutLeft"
				}
				_tar.innerText = 'Miss'
				break;
			default:
				break;
		}
		let play = () => {
			_tar.classList.add(..._class);
			const normalCallback = () => {
				_tar.innerText = '';
				_tar.classList.remove(..._class);
				_tar.removeEventListener('animationend', normalCallback)
			}
			_tar.addEventListener('animationend', normalCallback);
		}
		if (_tar.style.animationPlayState = "running") {
			setTimeout(play, 1000);
		} else {
			play()
		}
	}
}